package org.dey.serviceImpl;

import org.apache.commons.lang3.StringUtils;
import org.dey.mapper.NumbersMapper;
import org.dey.service.NumbersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service
public class NumbersServiceimpl implements NumbersService {

    @Autowired
    NumbersService numbersService;

    @Autowired(required = false)
    NumbersMapper numbersMapper;

    //获取时间编号字符串
    @Override
    public String dateStr(){
        Calendar now = Calendar.getInstance();
        String nian = now.get(Calendar.YEAR)+"";
        int yue = now.get(Calendar.MONTH)+1;
        int ri = now.get(Calendar.DAY_OF_MONTH);
        String yuestr = "";
        String ristr = "";
        if(yue<10){
            yuestr = "0"+yue+"";
        }else{
            yuestr = yue+"";
        }
        if(ri<10){
            ristr = "0"+ri+"";
        }else{
            ristr = ri+"";
        }
        String datastr = nian+yuestr+ristr;

        return datastr;
    }


    //会员编号(dFileid)
    @Override
    public String memberid(){
        //获取时间字符串
        String datestr = numbersService.dateStr();
        String memberid = datestr;
        //数据库根据自增id列降序取第一条数据编号
        String s1 = numbersMapper.memberid();
        if(s1==null){
            memberid=memberid+"001";
        }else{
            //如果有数据
            //截取后六位
            String substring = s1.substring(9,11);
            //将后六位转成整数后加一后再转成字符串作位新编号的后六位
            String housanwei = (Integer.parseInt(substring)+1)+"";
            String s = StringUtils.leftPad(housanwei, 3, "0");
            memberid = memberid + s;
        }
        return memberid;
    }

    //物料设计单编号(dM0duleid)
    @Override
    public String dM0duleid(){

        String dmoduleid = "200";

        //获取时间字符串
        String datestr = numbersService.dateStr();
        //数据库根据自增id列降序取第一条数据编号
        String s = numbersMapper.dM0duleid();
        if(s!=null&&s!=""){
            //截取时间字符串和后四位
            String substring = s.substring(3,11);
            //判断时间字符串与当前字符串是否相等 equals()
            if(datestr.equals(substring)){
                //相等后取后四位先转成整数加1再转成字符串
                String substring1 = s.substring(11,15);
                String strint = (Integer.parseInt(substring1)+1)+"";
                String str = StringUtils.leftPad(strint, 4, "0");
                dmoduleid = dmoduleid+datestr+str;
            }else{
                //，不相等改变时间字符串和后四位0001
                dmoduleid = dmoduleid + datestr+ "0001";
            }
        }else {
            dmoduleid = dmoduleid+datestr+"0001";
        }


        return dmoduleid;
    }

    //工序设计单编号(mDesignProcedureid)
    @Override
    public String mDesignProcedureid(){
        String mdesignprocedureid = "201";

        //获取时间字符串
        String datestr = numbersService.dateStr();
        //数据库根据自增id列降序取第一条数据编号
        String s = numbersMapper.mDesignProcedureid();

        if(s!=null&&s!=""){
            //截取时间字符串和后四位
            String substring = s.substring(3,11);
            //判断时间字符串与当前字符串是否相等 equals()
            if(datestr.equals(substring)){
                //相等后取后四位先转成整数加1再转成字符串
                String substring1 = s.substring(11,15);
                String strint = (Integer.parseInt(substring1)+1)+"";
                String str = StringUtils.leftPad(strint, 4, "0");
                mdesignprocedureid = mdesignprocedureid+datestr+str;
            }else{
                //，不相等改变时间字符串和后四位0001
                mdesignprocedureid = mdesignprocedureid + datestr+ "0001";
            }
        }else{
            mdesignprocedureid = mdesignprocedureid+datestr+"0001";
        }


        return mdesignprocedureid;
    }

    //生产计划编号(mapplyid)
    @Override
    public String mApplyid(){
        String mapplyid = "300";

        //获取时间字符串
        String datestr = numbersService.dateStr();
        //数据库根据自增id列降序取第一条数据编号
        String s = numbersMapper.mApplyid();

        if(s!=null&&s!=""){
            //截取时间字符串和后四位
            String substring = s.substring(3,11);
            //判断时间字符串与当前字符串是否相等 equals()
            if(datestr.equals(substring)){
                //相等后取后四位先转成整数加1再转成字符串
                String substring1 = s.substring(11,15);
                String strint = (Integer.parseInt(substring1)+1)+"";
                String str = StringUtils.leftPad(strint, 4, "0");
                mapplyid = mapplyid+datestr+str;
            }else{
                //，不相等改变时间字符串和后四位0001
                mapplyid = mapplyid + datestr+ "0001";
            }

        }else {
            mapplyid = mapplyid + datestr+ "0001";
        }

        return mapplyid;
    }

    //派工单编号(mManufactureid)
    @Override
    public String mManufactureid(){
        String mmanufactureid = "301";

        //获取时间字符串
        String datestr = numbersService.dateStr();
        //数据库根据自增id列降序取第一条数据编号
        String s = numbersMapper.mManufactureid();

        if(s!=null&&s!=""){
            //截取时间字符串和后四位
            String substring = s.substring(3,11);
            //判断时间字符串与当前字符串是否相等 equals()
            if(datestr.equals(substring)){
                //相等后取后四位先转成整数加1再转成字符串
                String substring1 = s.substring(11,15);
                String strint = (Integer.parseInt(substring1)+1)+"";
                String str = StringUtils.leftPad(strint, 4, "0");
                mmanufactureid = mmanufactureid+datestr+str;
            }else{
                //，不相等改变时间字符串和后四位0001
                mmanufactureid = mmanufactureid + datestr+ "0001";
            }
        }else{
            mmanufactureid = mmanufactureid + datestr+ "0001";
        }

        return mmanufactureid;
    }

    //库存编号(sCellid)
    @Override
    public String sCellid(){
        String scellid = "400";

        //获取时间字符串
        String datestr = numbersService.dateStr();
        //数据库根据自增id列降序取第一条数据编号
        String s = numbersMapper.sCellid();
       if(s!=null&&s!=""){
    //截取时间字符串和后四位
    String substring = s.substring(3,11);
    //判断时间字符串与当前字符串是否相等 equals()
    if(datestr.equals(substring)){
        //相等后取后四位先转成整数加1再转成字符串
        String substring1 = s.substring(11,15);
        String strint = (Integer.parseInt(substring1)+1)+"";
        String str = StringUtils.leftPad(strint, 4, "0");
        scellid = scellid+datestr+str;
    }else{
        //，不相等改变时间字符串和后四位0001
        scellid = scellid + datestr+ "0001";
    }
      }else{
           scellid = scellid + datestr+ "0001";
       }

        return scellid;
    }

    //入库编号(sGatherid)
    @Override
    public String sGatherid(){
        String sgatherid = "401";

        //获取时间字符串
        String datestr = numbersService.dateStr();
        //数据库根据自增id列降序取第一条数据编号
        String s = numbersMapper.sGatherid();
       if(s!=null&&s!=""){
    //截取时间字符串和后四位
    String substring = s.substring(3,11);
    //判断时间字符串与当前字符串是否相等 equals()
    if(datestr.equals(substring)){
        //相等后取后四位先转成整数加1再转成字符串
        String substring1 = s.substring(11,15);
        String strint = (Integer.parseInt(substring1)+1)+"";
        String str = StringUtils.leftPad(strint, 4, "0");
        sgatherid = sgatherid+datestr+str;
    }else{
        //，不相等改变时间字符串和后四位0001
        sgatherid = sgatherid + datestr+ "0001";
    }
      }else{
           sgatherid = sgatherid + datestr+ "0001";
       }

        return sgatherid;
    }

    //出库编号(sPayid)
    @Override
    public String sPayid(){
        String spayid = "402";

        //获取时间字符串
        String datestr = numbersService.dateStr();
        //数据库根据自增id列降序取第一条数据编号
        String s = numbersMapper.sPayid();

        if(s!=null&&s!=""){
            //截取时间字符串和后四位
            String substring = s.substring(3,11);
            //判断时间字符串与当前字符串是否相等 equals()
            if(datestr.equals(substring)){
                //相等后取后四位先转成整数加1再转成字符串
                String substring1 = s.substring(11,15);
                String strint = (Integer.parseInt(substring1)+1)+"";
                String str = StringUtils.leftPad(strint, 4, "0");
                spayid = spayid+datestr+str;
            }else{
                //，不相等改变时间字符串和后四位0001
                spayid = spayid + datestr+ "0001";
            }
        }else {
            spayid = spayid + datestr+ "0001";
        }


        return spayid;
    }
}
